package com.shixi.shixitraining.shixitraining.view;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.util.PermissionUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;

/**
 * Created by shenyan on 2018/9/17.
 */
public class ChoosePictureDialog  extends Dialog {



    private TextView btnSelectPhoto;
    private TextView btnCamera;
    private TextView btnCancel;
    private Listener mListener;

    public ChoosePictureDialog(Context context, Listener l) {
        super(context, R.style.style_dialog);
        setContentView(R.layout.layout_photo_dialog);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        mListener = l;

        btnCancel = (TextView) findViewById(R.id.choose_from_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dismiss();
            }
        });
        btnSelectPhoto = (TextView) findViewById(R.id.choose_from_album);

        if (!PermissionUtil.selfPermissionGranted(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                ||!PermissionUtil.selfPermissionGranted(context,Manifest.permission.READ_EXTERNAL_STORAGE)||
                !PermissionUtil.selfPermissionGranted(context, Manifest.permission.CAMERA)){
            ToastUtil.shortToast("请开启相应权限");
        }else {

            btnSelectPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    mListener.choosePic();//从手机相册选择
                }
            });

            btnCamera = (TextView) findViewById(R.id.choose_from_camera);
            btnCamera.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dismiss();
                    mListener.chooseCamera();  //拍一张
                }
            });
        }

    }

    public interface Listener {
        void choosePic();
        void chooseCamera();
    }

}
